//
//  YXErrorView.m
//  CaoShi
//
//  Created by aa on 2018/8/7.
//  Copyright © 2018年 杨旭. All rights reserved.
//

#import "YXErrorView.h"

@interface YXErrorView ()
@property (weak, nonatomic) UIImageView *imageView;
@property (weak, nonatomic) UILabel *titleLb;
@property (copy, nonatomic) NSString *imageName;
@property (copy, nonatomic) NSString *title;
@property (weak, nonatomic) UIButton *updataBtn;
@end

@implementation YXErrorView

- (instancetype)initWithFrame:(CGRect)frame ImageName:(NSString *)imageName Title:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageName = imageName;
        self.title = title;
        [self setup];
    }
    return self;
}

- (void)setup
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.imageName]];
    [self addSubview:imageView];
    self.imageView = imageView;
    
    UILabel *titleLb = [[UILabel alloc] init];
    titleLb.text = self.title;
    titleLb.textAlignment = NSTextAlignmentCenter;
    titleLb.textColor = [UIColor py_colorWithHexString:@"#666666"];
    titleLb.font = [UIFont systemFontOfSize:15];
    [self addSubview:titleLb];
    self.titleLb = titleLb;
    
    UIButton *updataBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [updataBtn setTitle:@"刷新" forState:(UIControlStateNormal)];
    [updataBtn setTitleColor:RGB(51, 51, 51) forState:(UIControlStateNormal)];
    updataBtn.layer.masksToBounds = YES;
    updataBtn.layer.cornerRadius = 3.0f;
    updataBtn.layer.borderColor = RGB(51, 51, 51).CGColor;
    updataBtn.layer.borderWidth = 1.0f;
    [updataBtn addTarget:self action:@selector(click) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:updataBtn];
    self.updataBtn = updataBtn;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.imageView.centerX = self.width / 2;
    self.imageView.centerY = 200;
    
    self.titleLb.frame = CGRectMake(0, CGRectGetMaxY(self.imageView.frame) + 20, self.width, 15);
    self.updataBtn.frame = CGRectMake((self.width - 100) / 2, CGRectGetMaxY(self.titleLb.frame) + 20, 100, 30);
}

- (void)click {
    
    if (self.clickUpdataBlock) {
        self.clickUpdataBlock();
    }
}

@end
